<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_COST_TVSP.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_M_COST_TVSP;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	 $forms =  $_SESSION['MSEARCH']['COST_FLIER'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['COST_FLIER'] = $forms;
}

$wherearr =  array();
/*
if ( isset( $forms['months'] ) &&  ( !empty( $forms['months'] ) ) ){
	$month = $forms['months']<10 ? "0".$forms['months'] : $forms['months'];
	$month_begin = $forms['year4'].$month."01";
	$month_end   = $forms['year4'].$month."31";
	$wherearr[] = "( sent_date >=  '{$month_begin}' AND sent_date <=  '{$month_end}' )";
	
} elseif ( isset( $forms['year4'] ) &&  ( !empty( $forms['year4'] ) ) ){
	$month_begin = $forms['year4']."0101";
	$month_end   = $forms['year4']."1231";
	$wherearr[] = "( sent_date >=  '{$month_begin}' AND sent_date <=  '{$month_end}' )";
}
*/

if ( isset( $forms['year4'] ) &&  ( !empty( $forms['year4'] ) ) ){
	$year4 = $forms['year4'] ;
	$wherearr[] = "b.year4 =  '{$year4}' ";
}

if ( isset( $forms['matrix_cd'] ) &&  ( !empty( $forms['matrix_cd'] ) ) ){
	$wherearr[] = sprintf("a.matrix_cd =  '%s' ",  $forms['matrix_cd'] );
	$martrix_cd   =  $forms['matrix_cd'];
}



if ( isset( $forms['electric_id'] ) &&  ( !empty( $forms['electric_id'] ) ) ){
	$wherearr[] = sprintf("a.electric_id =  '%s' ",  $forms['electric_id'] );
}
if ( isset( $forms['mater_id'] ) &&  ( !empty( $forms['mater_id'] ) ) ){
	$wherearr[] = sprintf("a.mater_id =  '%s' ",  $forms['mater_id'] );
}



if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
}else{
	$page['current'] = 1;
}

$page['item'] = PAGE_MAX;

if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );

	//表示ために整形開始
	foreach ($data as $k => $v ) {
		$data[$k]["flier_date"] = Class_PWD::int8_to_date($v["flier_date"]) ;
	}
}
//表示ために整形完了

$months = array();
$months[""] = PLEASE_SELECT_ARR;
for( $a = 1; $a <= 12; $a++ )
{
	$months[$a] = "&nbsp;".$a;
}
$smarty->assign('months', $months );

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$smarty->assign('year4',  Class_PWD::get_years());
$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR );
$smarty->assign('week', $week );
if ($login['staff_auth'] == "G-9" ) {
	$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR, $login['autoid'] );
}else {
	$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR);
}

$smarty->assign('electrics', $electrics );

$maters =  Class_M_OTHER::get_mater_array($forms["electric_id"], $forms["year4"], PLEASE_SELECT_ARR ) ;
$smarty->assign('maters', $maters );

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_tvsp_index.html');
?>